package com.dodoke.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dodoke.common.utils.bean.BeanUtils;
import com.dodoke.domain.WxGoods;
import com.dodoke.domain.WxImage;
import com.dodoke.domain.WxPrizeRecord;
import com.dodoke.domain.vo.WxPrizeRecordVo;
import com.dodoke.mapper.WxPrizeRecordMapper;
import com.dodoke.service.IWxPrizeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author dodoke
 * @date 2020-08-25
 */
@Service
public class WxPrizeRecordServiceImpl implements IWxPrizeRecordService
{
    @Autowired
    private WxPrizeRecordMapper wxPrizeRecordMapper;

    /**
     * 查询【当前用户的中奖纪录】
     *
     * @param openId 【用户身份标识】openId
     * @return 【中奖纪录】
     */
    @Override
    public List<WxPrizeRecordVo> selectWxPrizeRecordByOpenId(String openId)
    {
        List<WxPrizeRecord> wxPrizeRecords = wxPrizeRecordMapper.selectWxPrizeRecordByOpenId(openId);
        ArrayList<WxPrizeRecordVo> wxPrizeRecordVos = new ArrayList<>();
        String imageUrl = null;
        for (WxPrizeRecord wxPrizeRecord : wxPrizeRecords) {
            WxPrizeRecordVo wxPrizeRecordVo = new WxPrizeRecordVo();
            for (WxGoods wxGoods : wxPrizeRecord.getWxGoodsList()) {

                BeanUtils.copyBeanProp(wxPrizeRecordVo,wxGoods);
            }
            WxImage wxImage = wxPrizeRecord.getWxImage();
            if (wxImage != null){
                imageUrl = wxImage.getImageUrl();
                wxPrizeRecordVo.setImageUrl(imageUrl);
            }
            wxPrizeRecordVo.setType(wxPrizeRecord.getType());
            wxPrizeRecordVos.add(wxPrizeRecordVo);
        }
        return wxPrizeRecordVos;
    }

    /**
     * 插入【当前用户的中奖纪录】
     *
     * @param wxPrizeRecord
     */
    @Override
    public int insertWxPrizeRecordByOpenId(WxPrizeRecord wxPrizeRecord) {
        int i = wxPrizeRecordMapper.insertWxPrizeRecordByOpenId(wxPrizeRecord);
        return i;
    }

}
